package com.music.activity.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.anmusic.application.R;
import com.music.model.Mp3Info1;
import com.music.model.SongInfo;
import com.music.util.ChangeCharset;

public class MusicListAdapter extends BaseAdapter {
	private List<SongInfo> mp3List;
	private LayoutInflater inflater;

	public MusicListAdapter(Context context, List<SongInfo> mp3List) {
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mp3List = mp3List;
	}

	@Override
	public int getCount() {

		return mp3List.size();
	}

	@Override
	public SongInfo getItem(int arg0) {
		return mp3List.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.music_list_item, null);
			viewHolder.musicNameTv = (TextView) convertView
					.findViewById(R.id.TextView_name);
			viewHolder.musicTimeTv = (TextView) convertView
					.findViewById(R.id.TextView_time);
			viewHolder.musicArtistTv = (TextView) convertView
					.findViewById(R.id.TextView_artist);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		SongInfo songInfo = getItem(arg0);
		viewHolder.musicNameTv.setText(songInfo.getTitle());
		viewHolder.musicTimeTv.setText(String.valueOf(songInfo.getDuration()));
		viewHolder.musicArtistTv.setText(songInfo.getArtist());
		return convertView;
	}

	private class ViewHolder {
		private TextView musicNameTv;
		private TextView musicTimeTv;
		private TextView musicArtistTv;
	}
}
